//
//  HomeBannerView.m
//  CakeClient
//
//  Created by scjh on 15/3/9.
//  Copyright (c) 2015年 SCJH. All rights reserved.
//

#import "HomeBannerView.h"
#import "HomeBannerCollectionViewCell.h"
#import "SMPageControl.h"

static NSString *bannerCellID = @"HomeBannerCollectionViewCell";
@interface HomeBannerView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSInteger _lastPage;
}
@property(nonatomic,assign)NSInteger allNum;
@property(nonatomic,strong)SMPageControl *pageControl;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)BannerSelectBlock block;

@end
@implementation HomeBannerView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self UI];
        
    }
    return self;
}

#pragma mark 设置属性

- (void)setImageS:(NSArray *)imageS {
    
    _imageS = imageS;
    self.allNum = imageS.count;
    self.pageControl.numberOfPages = self.allNum;
    [self resetPageControl];
    
}

- (void)setCurrIndex:(NSInteger)currIndex {
    
    _currIndex = currIndex;
    NSInteger currNum = self.collectionView.contentOffset.x / CGRectGetWidth(self.collectionView.frame);
    self.pageControl.currentPage = currIndex;
    self.collectionView.contentOffset = CGPointMake(CGRectGetWidth(self.collectionView.frame) * (currNum + currIndex - 1), 0);
    _lastPage = self.pageControl.currentPage;
}

- (void)setPageState:(PageControlState)pageState {
    
    _pageState = pageState;
    [self resetPageControl];
}

/**
 *  重置pageconrl的frame
 */
- (void)resetPageControl {
    
    CGRect frame = self.pageControl.frame;
    switch (_pageState) {
        case PageControlStateOne:
            
            frame.size.width = 20 * self.allNum;
            break;
        case PageControlStateTwo:
            
            frame.size.width = CGRectGetWidth(self.collectionView.frame);
            frame.origin.x = 0;
            break;
        case PageControlStateNone:
            frame.size.height = 0;
            CGRect contentFrame = self.collectionView.frame;
            contentFrame.size.height = CGRectGetHeight(self.frame);
            self.collectionView.frame = contentFrame;
            break;
        default:
            break;
    }
    
    self.pageControl.frame = frame;
    [self.collectionView reloadData];
}

#pragma mark 搭建UI界面

- (void)UI {
    
    self.allNum = 2;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    CGRect frame = self.frame;
    frame.size.height -= 20;
    self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeBannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:bannerCellID];
    [self.collectionView setContentOffset:CGPointMake(CGRectGetWidth(self.collectionView.frame) * 10000, 0)];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    

    self.pageControl = [[SMPageControl alloc] initWithFrame:CGRectMake(20, CGRectGetHeight(self.collectionView.frame) , 100, 20)];
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = self.allNum;
    self.pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"轮播-选中"];
    self.pageControl.pageIndicatorImage = [UIImage imageNamed:@"轮播-未选中"];
    [self addSubview:self.pageControl];
    [self.pageControl addTarget:self action:@selector(pageControlToContent:) forControlEvents:UIControlEventValueChanged];
}

#pragma mark collectionView的代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 20000;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:bannerCellID forIndexPath:indexPath];
//    cell.cellImageView.image = LOAD_IMAGE(@"shoplogo.png");
    if (self.imageS.count > 0) {
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",kUrl,self.imageS[indexPath.row % 2][@"image"]];
        [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    }
    
    cell.backgroundColor = [UIColor redColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.block) {
        self.block(self.pageControl.currentPage);
    }
}

#pragma mark scrollView的代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger currNum = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
    self.pageControl.currentPage = currNum % self.allNum;
    _lastPage = self.pageControl.currentPage;
}


#pragma mark pageControl改变的事件

- (void)pageControlToContent:(UIPageControl *)pageControl {
    
    NSInteger currNum = self.collectionView.contentOffset.x / CGRectGetWidth(self.collectionView.frame);
    if (_lastPage < pageControl.currentPage) {
        
        currNum ++;
    }else {
        
        currNum --;
    }
    CGPoint currPoint = CGPointMake(CGRectGetWidth(self.collectionView.frame) * (currNum), 0);
    [self.collectionView setContentOffset:currPoint animated:YES];
    _lastPage = self.pageControl.currentPage;
}

- (void)bannerViewSelect:(BannerSelectBlock)selectBlock {
    
    self.block = selectBlock;
}

@end
